package com.xuecheng.basic.service.impl;

import com.xuecheng.basic.domain.po.Category;
import com.xuecheng.basic.mapper.CategoryMapper;
import com.xuecheng.basic.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2024-05-02
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /*
    * 查询所有分类
    * */
    @Override
    public ResponseResult<CategoryVo> findAll() {
        //1.查询一级分类
        Category one = this.lambdaQuery().eq(Category::getParentid, "0").one();
        //1-1 转vo
        CategoryVo oneVo = BeanHelper.copyProperties(one, CategoryVo.class);
        //2.查询二级分类
        List<Category> twoList = this.lambdaQuery().eq(Category::getParentid, oneVo.getId()).list();
        //2-1 转vo
        List<CategoryVo> twoVoList = BeanHelper.copyWithCollection(twoList, CategoryVo.class);
        //2-2将二级设置到一级中
        oneVo.setCategoryTreeNodes(twoVoList);
        //3.查询三级分类
        for (CategoryVo twoVo : twoVoList) {
            List<Category> threeList = this.lambdaQuery().eq(Category::getParentid, twoVo.getId()).list();
            //3-1转vo
            List<CategoryVo> threeVoList = BeanHelper.copyWithCollection(threeList, CategoryVo.class);
            //3-2将三界设置到二级中
            twoVo.setCategoryTreeNodes(threeVoList);
        }
        //4.返回以及分类vo
        return ResponseResult.okResult(oneVo);
    }
}
